package com.xposed.openfire.smack.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xposed.openfire.smack.APPOpenfireSmack;
import com.xposed.openfire.smack.R;
import com.xposed.openfire.smack.adapter.ChatRecordAdapter;
import com.xposed.openfire.smack.entity.ChatMessage;
import com.xposed.openfire.smack.entity.ChatMessageDao;
import com.xposed.openfire.smack.entity.ChatRecordDao;
import com.xposed.openfire.smack.utils.JsonUtils;
import com.xposed.openfire.smack.utils.LogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;

public class ChatFragment extends Fragment implements ChatRecordAdapter.OnItemClickListener {

    Unbinder unbinder;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private ChatRecordAdapter chatRecordAdapter;

    private ChatRecordDao chatRecordDao;

    private CompositeDisposable mDisposables = new CompositeDisposable();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, container, (false));
        unbinder = ButterKnife.bind((this), view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        chatRecordDao = APPOpenfireSmack.getDaoSession().getChatRecordDao();

        chatRecordAdapter = new ChatRecordAdapter(getActivity(), chatRecordDao.loadAll());
        chatRecordAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(chatRecordAdapter);


        List<ChatMessage> messages = APPOpenfireSmack.getDaoSession().getChatMessageDao()
                .queryBuilder().where(ChatMessageDao.Properties.Name.eq("admin")).list();

        LogUtils.e(JsonUtils.objectToString(messages));

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
